--往昔弹丸
local wangxidanwan = fk.CreateSkill {
    name = "mcdh_wangxidanwan",
    tags={Skill.Compulsory}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

wangxidanwan:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(self) then return false end
        local room = player.room
        local move_event = room.logic:getCurrentEvent()
        local parent_event = move_event.parent
        local card_ids = {}
        if parent_event ~= nil then
            if parent_event.event == GameEvent.UseCard or parent_event.event == GameEvent.RespondCard then
                local parent_data = parent_event.data[1]
                --使用者不是自己
                if parent_data.from ~= player.id then
                    card_ids = room:getSubcardsByRule(parent_data.card)
                end
            elseif parent_event.event == GameEvent.Pindian then
                local pindianData = parent_event.data[1]
                if pindianData.from ~= player then
                    card_ids = room:getSubcardsByRule(pindianData.fromCard)
                else
                    for toId, result in pairs(pindianData.results) do
                        if player.id ~= toId then
                            table.insertIfNeed(card_ids,room:getSubcardsByRule(result.toCard))
                        end
                    end
                end
            end
        end
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                if  move.from and move.from ~= player.id then
                    for _, info in ipairs(move.moveInfo) do
                        if Fk:getCardById(info.cardId).name == "collateral" then
                            return true
                        end
                    end
                elseif #card_ids > 0 then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
                                Fk:getCardById(info.cardId).name == "collateral" then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local move_event = room.logic:getCurrentEvent()
        local parent_event = move_event.parent
        local card_ids = {}
        local get= {}
        if parent_event ~= nil then
            if parent_event.event == GameEvent.UseCard or parent_event.event == GameEvent.RespondCard then
                local parent_data = parent_event.data[1]
                --使用者不是自己
                if parent_data.from ~= player.id then
                    card_ids = room:getSubcardsByRule(parent_data.card)
                end
            elseif parent_event.event == GameEvent.Pindian then
                local pindianData = parent_event.data[1]
                if pindianData.from ~= player then
                    card_ids = room:getSubcardsByRule(pindianData.fromCard)
                else
                    for toId, result in pairs(pindianData.results) do
                        if player.id ~= toId then
                            table.insertIfNeed(card_ids,room:getSubcardsByRule(result.toCard))
                        end
                    end
                end
            end
        end
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                if  move.from ~= player.id then
                    for _, info in ipairs(move.moveInfo) do
                        if Fk:getCardById(info.cardId).name == "collateral" then
                            table.insertIfNeed(get ,info.cardId)
                        end
                    end
                elseif #card_ids > 0 then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
                                Fk:getCardById(info.cardId).name == "collateral" then
                            table.insertIfNeed(get ,info.cardId)
                        end
                    end
                end
            end
        end
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(get)
        room:obtainCard(player.id, dummy, false, fk.ReasonJustMove)
        lib.LoadAmmo(player,player,lib.mcdh_getLoseammo(player),self.name)
    end
})

Fk:loadTranslationTable{
    ["mcdh_wangxidanwan"] ="往昔弹丸",
    [":mcdh_wangxidanwan"] = "锁定技，当【借刀杀人】从你的区域外进入弃牌堆后，你获得此牌并装填弹药至上限。"
}

return wangxidanwan